﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using DataLayer.Types;
using Microsoft.Windows.Controls;
using ViewModel.Aggregators;
using ViewModel.Finance;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for BudgetItemsListView.xaml
    /// </summary>
    public partial class BudgetItemsListView : UserControl
    {
        private BudgetItemsListViewModel viewModel;

        public BudgetItemsListView()
        {
            InitializeComponent();

            viewModel = DataContext as BudgetItemsListViewModel;
        }

        public ComboBox PeriodCombo
        {
            get
            {
                return cboPaymentPeriod;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            SetupPaymentPeriodBinding();
            LoadGroupingComboBox();
        }

        private void LoadGroupingComboBox()
        {
            cboGrouping.Items.Add("None");
            cboGrouping.Items.Add("Category");
            cboGrouping.Items.Add("Frequency");
            cboGrouping.SelectedIndex = 0;
        }

        private void SetupPaymentPeriodBinding()
        {
            DataGridTextColumn colPaymentPeriod = new DataGridTextColumn();
            colPaymentPeriod.Header = "Period";

            MultiBinding bdgMulti = new MultiBinding();
            bdgMulti.Converter = new PaymentPeriodAggregator();
            bdgMulti.Bindings.Add(new Binding("Amount"));
            bdgMulti.Bindings.Add(new Binding("Frequency.ValuePerYear"));
            Binding b = new Binding();
            b.Source = cboPaymentPeriod;
            b.Path = new PropertyPath("SelectedItem");
            bdgMulti.Bindings.Add(b);
            bdgMulti.Mode = BindingMode.OneWay;
            colPaymentPeriod.Binding = bdgMulti;

            dataGrid1.Columns.Add(colPaymentPeriod);
        }

        private void dataGrid1_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void ShowItemPropertiesDialog()
        {
            BudgetItem item = dataGrid1.SelectedItem as BudgetItem;
            ShowItemDialog(item);
        }

        private void ShowItemDialog(BudgetItem item)
        {
            BudgetItemPropertiesView propertiesView = new BudgetItemPropertiesView();
            propertiesView.DataContext = new BudgetItemViewModel(item);
            propertiesView.ShowDialog();
        }

        private void GroupColumns(string columnName)
        {
            CollectionView defaultView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) as CollectionView;
            if(defaultView.CanGroup == false) return;
            PropertyGroupDescription groupDescription = new PropertyGroupDescription(columnName);
            defaultView.GroupDescriptions.Add(groupDescription);
        }

        private void ClearColumnGrouping()
        {
            CollectionView defaultView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) as CollectionView;
            if(defaultView == null) return;
            defaultView.GroupDescriptions.Clear();
        }

        private void CboGrouping_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClearColumnGrouping();
            string columName = cboGrouping.SelectedValue as string;
            if(columName == "None") return;
            GroupColumns(columName);
        }

        private void ExpandGroups_Click(object sender, RoutedEventArgs e)
        {
            CollectionView defaultView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource) as CollectionView;
            foreach(CollectionViewGroup group in defaultView.Groups)
            {
//                group.
//                foreach (ElementViewModel o in group.Items)
//                {
//                    
//                }
//                group.Items
            }
        }

        private void CollapseGroups_Click(object sender, RoutedEventArgs e)
        {
        }

        private void ItemPropertiesClicked(object sender, RoutedEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void NewItemClicked(object sender, RoutedEventArgs e)
        {
            BudgetItem item = new BudgetItem();
            ShowItemDialog(item);
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DeleteItem(dataGrid1.SelectedItem as BudgetItem);
        }

        private void DeleteItem(BudgetItem item)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this item?\n\r\n\r" + item.Name,
                                                      "Delete Item", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if(result != MessageBoxResult.OK) return;

            viewModel.DeleteItem(item);
        }

        private void CreateSavingsPlan(object sender, RoutedEventArgs e)
        {
            BudgetItem budgetItem = dataGrid1.SelectedItem as BudgetItem;
            SavingsPlan item = new SavingsPlan();
            item.StartDate = DateTime.Now;
            item.EndDate = DateTime.Now.AddDays(7);
            item.Name = budgetItem.Name + " (fbi)";
            item.TargetAmount = budgetItem.Amount;
            item.Active = true;
            SavingsPlanPropertiesView propertiesView = new SavingsPlanPropertiesView();
            propertiesView.DataContext = new SavingsPlanPropertiesViewModel(item);
            propertiesView.ShowDialog();
        }

    }
}
